<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户" prop="merchantId">
        <el-select v-model="queryParams.merchantId" placeholder="请选择商户" clearable size="small">
          <el-option  v-for="merchant in merchantList"
                      :key="merchant.deptId"
                      :label="merchant.deptName"
                      :value="merchant.deptId" />
        </el-select>
      </el-form-item>
      <el-form-item label="执行状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择任务执行状态" clearable size="small">
          <el-option label="待执行" value="0" />
          <el-option label="执行中" value="1" />
          <el-option label="执行成功" value="2" />
          <el-option label="执行失败" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

      <el-row :gutter="40" class="panel-group">
        <!--视频数据-->
        <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
          <div class="card-panel" @click="handleSetLineChartData('messages')">
            <div class="card-panel-icon-wrapper icon-people">
              <svg-icon icon-class="peoples" class-name="card-panel-icon" />
            </div>
            <div class="card-panel-description">
              <div class="card-panel-text">
                参与人数
              </div>
              <count-to :start-val="0" :end-val="countData.canyu" :duration="3000"  class="card-panel-num" />
            </div>
          </div>
        </el-col>
        <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
          <div class="card-panel" @click="handleSetLineChartData('newVisitis')">
            <div class="card-panel-icon-wrapper icon-play">
              <svg-icon icon-class="play" class-name="card-panel-icon" />
            </div>
            <div class="card-panel-description">
              <div class="card-panel-text">
                累计播放数
              </div>
              <count-to :start-val="0" :end-val="countData.bofang" :duration="2600" class="card-panel-num" />
            </div>
          </div>
        </el-col>
        <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
          <div class="card-panel" @click="handleSetLineChartData('newVisitis')">
            <div class="card-panel-icon-wrapper icon-dianzan">
              <svg-icon icon-class="dianzan" class-name="card-panel-icon" />
            </div>
            <div class="card-panel-description">
              <div class="card-panel-text">
                累计点赞数
              </div>
              <count-to :start-val="0" :end-val="countData.dianzan" :duration="2600" class="card-panel-num" />
            </div>
          </div>
        </el-col>
        <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
          <div class="card-panel" @click="handleSetLineChartData('newVisitis')">
            <div class="card-panel-icon-wrapper icon-pinglun">
              <svg-icon icon-class="pinglun" class-name="card-panel-icon" />
            </div>
            <div class="card-panel-description">
              <div class="card-panel-text">
                累计评论数
              </div>
              <count-to :start-val="0" :end-val="countData.pinglun" :duration="2600" class="card-panel-num" />
            </div>
          </div>
        </el-col>
      </el-row>
    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="商户" align="center" prop="deptName" />
      <el-table-column label="关联活动" align="center" prop="activityTitle" />
      <el-table-column label="转发人" align="center" prop="nickname" />
      <el-table-column label="执行状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0" type="warning">待执行</el-tag>
          <el-tag v-if="scope.row.status == 1" type="info">执行中</el-tag>
          <el-tag v-if="scope.row.status == 2" type="success">执行成功</el-tag>
          <el-tag v-if="scope.row.status == 3" type="danger">执行失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="执行时间" align="center" prop="createTime" width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-star-on"
            @click="dyPublishDetail(scope.row.id)"
          >点赞数据</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!-- 查询抖音发送详情对话框 -->
    <el-dialog title="抖音推送记录" :visible.sync="dyPublishFormOpen" width="900px" append-to-body>
      <el-table v-loading="taskDyPublishListLoading" :data="taskDyPublishList">
        <el-table-column label="转发状态" align="center" prop="status">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == 1" type="success">发布成功</el-tag>
            <el-tag v-if="scope.row.status == 2" type="danger">发布失败</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="点赞数" align="center" prop="diggCount" />
        <el-table-column label="下载数" align="center" prop="downloadCount" />
        <el-table-column label="转发数" align="center" prop="forwardCount" />
        <el-table-column label="播放数" align="center" prop="playCount" />
        <el-table-column label="分享数" align="center" prop="shareCount" />
        <el-table-column label="评论数" align="center" prop="commentCount" />
        <el-table-column label="转发时间" align="center" prop="createTime" width="200">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { listTask, getTask, delTask, addTask, updateTask, exportTask,getStatistics } from "@/api/task/task";
import { listTaskDyPublish } from "@/api/task/taskDyPublish";
import CountTo from 'vue-count-to';
import { listDept } from "@/api/system/dept";
export default {
  components: {
    CountTo,
  },
  name: "Task",
  data() {
    return {
      countData:{
        canyu:0,
        bofang:0,
        dianzan:0,
        pinglun:0,
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 待发布视频的任务表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        status: null,
        activityId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      dyPublishForm:{},
      // 抖音推送记录表格数据
      taskDyPublishList: [],
      // 遮罩层
      taskDyPublishListLoading: true,
      dyPublishFormOpen:false,
      //商户列表
      merchantList: [],

    };
  },
  created() {
    this.getList();
    this.getMerchantList();

    //查询顶部统计数据
    this.getStatistics();
  },
  methods: {
    /** 查询顶部统计数据 */
    getStatistics(){
      getStatistics(this.queryParams).then(response => {
        this.countData = response;
      });
    },

    /** 查询商户列表 */
    getMerchantList() {
      this.loading = true;
      listDept({deptType:2}).then(response => {
        this.merchantList = response.data;
        this.loading = false;
      });
    },
    /** 查询待发布视频的任务列表 */
    getList() {
      this.loading = true;
      listTask(this.queryParams).then(response => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        createTime: null,
        updateTime: null,
        status: 0,
        activityId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      //查询顶部统计数据
      this.getStatistics();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加待发布视频的任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTask(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改待发布视频的任务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTask(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTask(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除待发布视频的任务编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTask(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有待发布视频的任务数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTask(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    /**查看抖音转发记录*/
    dyPublishDetail(id){
      this.taskDyPublishListLoading = true;
      listTaskDyPublish({"taskId":id}).then(response => {
        this.dyPublishFormOpen = true;
        this.taskDyPublishList = response.rows;
        this.taskDyPublishListLoading = false;
      });


    }
  }
};
</script>

<style scoped lang="scss">
  .panel-group {
    margin-top: 18px;

    .card-panel-col {
      margin-bottom: 32px;
    }

    .card-panel {
      height: 108px;
      cursor: pointer;
      font-size: 12px;
      position: relative;
      overflow: hidden;
      color: #666;
      background: #fff;
      box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
      border-color: rgba(0, 0, 0, .05);

      &:hover {
        .card-panel-icon-wrapper {
          color: #fff;
        }

        .icon-people {
          background: #34bfa3;
        }
        .icon-play {
          background: #34bfa3
        }
        .icon-dianzan {
          background: #34bfa3
        }
        .icon-pinglun {
          background: #34bfa3
        }
      }

      .icon-people {
        color: #40c9c6;
      }

      .icon-play {
        color: #40c9c6;
      }

      .icon-dianzan {
        color: #40c9c6;
      }

      .icon-pinglun {
        color: #34bfa3
      }

      .card-panel-icon-wrapper {
        float: left;
        margin: 14px 0 0 14px;
        padding: 16px;
        transition: all 0.38s ease-out;
        border-radius: 6px;
      }

      .card-panel-icon {
        float: left;
        font-size: 48px;
      }

      .card-panel-description {
        float: right;
        font-weight: bold;
        margin: 26px;
        margin-left: 0px;

        .card-panel-text {
          line-height: 18px;
          color: rgba(0, 0, 0, 0.45);
          font-size: 16px;
          margin-bottom: 12px;
        }

        .card-panel-num {
          font-size: 20px;
        }
      }
    }
  }

  @media (max-width:550px) {
    .card-panel-description {
      display: none;
    }

    .card-panel-icon-wrapper {
      float: none !important;
      width: 100%;
      height: 100%;
      margin: 0 !important;

      .svg-icon {
        display: block;
        margin: 14px auto !important;
        float: none !important;
      }
    }
  }
</style>
